/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
angular.module("apply", ['baseModule'])
        .controller('mainCtrl', function ($scope, base) {
            $scope.type = type;
            if (ask_code != 'null') {
                $scope.saveFlag = true;
                base.post("getDetail", {ask_code: ask_code}).get(function (data) {
                    $scope.info = data;
                    if ($scope.info.recvicer_id) {
                        $scope.saveFlag = true;
                    } else {
                        $scope.saveFlag = false;
                        $('.manager_x').removeAttr("disabled");
                    }
                    //console.log($scope.info);
                })

            } else {
                $scope.info = info;
                $scope.info.sender_id = userInfo.username;
                $scope.info.sender_name = userInfo.ch_name;
                $scope.info.sender_phone = userInfo.phone;
                // $scope.info.first_diagnosis ="1111111";
            }
            $scope.modal = null;
            $scope.openYZ = function (type, modal) {
                $(".mode-info").modal('show');
                $scope.modal = modal;
                base.post("getInfo", {host_id: $scope.info.host_id, type: type}).get(function (data) {
                    $scope.infoData = data;
                })
            }
            $scope.sureData = function () {
                var arr = [];
                for (var i in $scope.infoData) {
                    if ($scope.infoData[i].flag) {
                        arr.push($scope.infoData[i].pk);
                    }
                }
                $scope.info[$scope.modal] = arr.join(",");
                $scope.info[$scope.modal + '_count'] = arr.length;
                $(".mode-info").modal('hide');
            }
            $scope.saveApply = function () {
                if (type == 'create') {
                    base.post("saveApply", {data: angular.toJson($scope.info)}).get(function (data) {
                        if (data != 'false') {
                            steveAl("保存成功", 1000);
                            $scope.info.ask_code = data.ask_code;
                            $scope.info.ask_pk = data.ask_pk;
                        }
                    })
                } else if (type == 'manager') {

                    if (!$scope.info.appointment_time) {
                        steveAl("请选择会诊时间", 1000);
                    } else if (!$scope.info.recvicer_name) {
                        steveAl("请选择会诊专家", 1000);
                    } else {
                        steveConfrim("确定保存吗？", function () {
                            base.post("updateAskDetail", $scope.info).get(function (data) {
                                if (data == 'true') {
                                    steveAl("保存成功", 1000);
                                } else {
                                    steveAl(data, 1000);
                                }
                            })
                        },null,true);

                    }

                }
                console.log($scope.info);
            }
            $scope.openDoctorM = function () {
                console.log("123123");
                $(".mode-doctor").modal('show');
                base.post("getDoctor").get(function (data) {
                    $scope.ds = data.data;
                })
            }
            $scope.selectDoctor = function (item, event) {
                //$th = event.target;
                $(event.target).parent().siblings().removeClass("bg-success");
                $(event.target).parent().addClass("bg-success");
                $(event.target).removeClass("bg-success");
                $scope.info.recvicer_id = item.username;
                $scope.info.recvicer_name = item.ch_name;
                $scope.info.recvicer_phone = item.phone;
                $("#doctor").val(item.ch_name);
                $("#doctor_phone").val(item.phone);

            }

            var web;
            var name = new Date().getTime() + "cx";

            var login = function () {
                if (!web) {

                    // web = new WebSocket("ws://60.161.186.217:33888/websocket");
                    web = new WebSocket("ws://222.219.119.189:13134/websocket/" + name);
                    //web = new WebSocket("ws://127.0.0.1:19090/websocket/" + name);
                }
                web.onopen = function () {
                    console.log("open");
                    var msg = {type: "getInfo", from: name, to: info.hospital_code, body: info.info_id};
                    //var msg = {type: "getInfo", from: name, to: "CX0712131CN1926", body: "2016003569"};
                    web.send(JSON.stringify(msg));
                }
                web.onmessage = function (e) {

                    console.log(JSON.parse(e.data));
                    var remoteInfo = JSON.parse(e.data);
                    if (remoteInfo.type == 'getInfo') {
                        $scope.info.first_diagnosis = remoteInfo.sDiagnosis;
                        $scope.info.main_diagnosis = remoteInfo.sMainSuit;
                        $scope.info.now_doc = remoteInfo.sPresentHistory;
                        $scope.info.body_check = remoteInfo.sBodyCheck;
                        $scope.info.body_check = remoteInfo.sBodyCheck;
                        $scope.$apply();
                        console.log($scope.info);
                    }

                };

                web.onclose = function (e) {
                    web = null;
                    console.log("close");
                };
                web.onerror = function (e) {

                };
            }
            login();
        });


